//
//  GovrecordsViewController.m
//  CarFile
//
//  Created by 陈浩 on 2018/2/27.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "GovrecordsViewController.h"
#import "JL_Model.h"
#import "CHWXJLCell.h"
@interface GovrecordsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,assign) NSInteger index;
@property (nonatomic ,strong) NSMutableArray *datas;
@end

@implementation GovrecordsViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"维修记录查询";
    
    [self setupTableView];
    
    [self getNetworkData];
}


//  建立列表UI
-(void)setupTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH  -64) style:UITableViewStylePlain];
    // 设置tableView的基本信息
    
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    // 下拉刷新
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getNetworkData];
        
    }];
    //        //     上拉刷新
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}


#pragma mark -- 网络请求数据
-(void)getNetworkData{
    _index = 1;
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"carno"] = _carno;
    params[@"carvin"] = _carvin;
    params[@"usertype"] = (NSString *)[Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    if (_carno.length > 0) {
       params[@"sign"] = [DESCript encrypt:_carno encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }else{
       params[@"sign"] = [DESCript encrypt:_carvin encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
   
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"10";
    NSLog(@"%@",params);
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovrecords" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        NSLog(@"%@",result);
        [MBProgressHUD hideHUD];
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                JL_Model *model = [JL_Model objectWithKeyValues:dict];
                
                [self.datas addObject:model];
            }
            if (self.datas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            if (self.datas.count == 0) {
                
                self.tableView.placeholder = @"非常抱歉，没有查到相关维修记录";
                self.tableView.placeholderImage = @"data_null";
            }
            
            
        }else{
            
            
            [MBProgressHUD showTipMessageInView:result[@"status"]];
         
            self.tableView.placeholder = @"非常抱歉，没有查到相关维修记录";
            self.tableView.placeholderImage = @"data_null";
            
        }
        
        [self.tableView reloadData];
        
    }];
    
}

#pragma mark --  加载更多数据
-(void)loadMoreData{
    _index++;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"carno"] = _carno;
    params[@"carvin"] = _carvin;
    params[@"usertype"] = (NSString *)[Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    if (_carno) {
        params[@"sign"] = [DESCript encrypt:_carno encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }else{
        params[@"sign"] = [DESCript encrypt:_carvin encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"10";
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovrecords" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [self.tableView.mj_footer endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            NSMutableArray *newDatas = [NSMutableArray array];
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                JL_Model *model = [JL_Model objectWithKeyValues:dict];
                
                [newDatas addObject:model];
            }
            if (newDatas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.datas addObjectsFromArray:newDatas];
            
        }else{
            
            self.tableView.placeholder = @"非常抱歉，没有查到相关维修记录";
            self.tableView.placeholderImage = @"data_null";
            
        }
        
        [self.tableView reloadData];
        
    }];
}



#pragma mark - UITableView数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHWXJLCell *cell = [CHWXJLCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = [LongriseRouter openURL:@"CHWXJLDetailsViewController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            
            params[@"model"] = self.datas[indexPath.row];
            
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                
                
            }];
        }
    }
    
}


//补全分割线
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell  respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
    {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


@end
